﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Account_Profile : System.Web.UI.Page
{
    static UserLogin user;
    protected void Page_Load(object sender, EventArgs e)
    {
        user = (UserLogin)Session["User"];

        if (user == null)
        {
            Response.Redirect("~/UserLogin.aspx");
        }

        if (!IsPostBack)
        {
            LoadPlaylist();
        }
    }

    protected void LoadPlaylist()
    {
        Sharemp3DataContext db = new Sharemp3DataContext();
        int userid = user.UserID;

        var playlist = from i in db.Playlists
                       where i.UserID == userid
                       orderby i.CreateDate descending
                       select new
                       {
                           PlaylistID = i.PlaylistID,
                           UserID = i.UserID,
                           Title = i.Title,
                           NumMusic = i.PlaylistMusics.Count(),
                           CreateDate = i.CreateDate
                       };
        Rpt_Playlist.DataSource = playlist;
        Rpt_Playlist.DataBind();
    }

    protected void AddNewPlaylist(object sender, EventArgs e)
    {
        int userid = user.UserID;
        string title = txtPlaylist.Text;
        if (!String.IsNullOrEmpty(title))
        {
            Sharemp3DataContext db = new Sharemp3DataContext();

            int n = db.Playlists.Where(i => i.UserID == userid && i.Title == title).Count();

            if (n == 0)
            {
                Playlist p = new Playlist { UserID = userid, Title = title, CreateDate = DateTime.Now };
                try
                {
                    db.Playlists.InsertOnSubmit(p);
                    db.SubmitChanges();
                    LoadPlaylist();
                }
                catch
                {
                    lblResult.InnerText = "fail !";
                }
            }
            else
            {
                lblResult.InnerText = "duplicate name !";
            }
        }
    }

    protected void LoadPlaylistMusic(object sender, CommandEventArgs e)
    {
        Sharemp3DataContext db = new Sharemp3DataContext();

        int playlistid = Convert.ToInt32(e.CommandArgument);

        var musiclst = (from i in db.PlaylistMusics
                        where i.PlaylistID == playlistid
                        select new
                        {
                            MusicID = i.MusicID,
                            Title = i.Music.Title,
                            AlbumID = i.Music.AlbumID,
                            AlbumName = i.Music.Album.AlbumName,
                            ArtistID = i.Music.Album.ArtistID,
                            ArtistName = i.Music.Album.Artist.ArtistName,
                            Rating = i.Music.Rating,
                            Cover = i.Music.Cover,
                            Date = i.Music.Album.Date
                        }).OrderByDescending(i => i.Date);

        Rpt_MusicList.DataSource = musiclst;
        Rpt_MusicList.DataBind();

        playlistTitle.InnerText = db.Playlists.Where(i => i.PlaylistID == playlistid).First().Title + " - " + musiclst.Count() + " items";

        Playlistmusic.Style["display"] = "block";
    }

    protected void DeletePlaylistMusic(object sender, CommandEventArgs e)
    {
        Sharemp3DataContext db = new Sharemp3DataContext();

        int playlistid = Convert.ToInt32(e.CommandArgument);

        db.DeletePlaylist(playlistid);
        db.SubmitChanges();

        Response.Redirect("~/Profile.aspx");
    }
}